package com.taoyuan.hotel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taoyuan.hotel.dao.OrderDao;
import com.taoyuan.hotel.entity.Order;
import com.taoyuan.hotel.service.intf.OrderService;
import com.taoyuan.hotel.vo.PageBean;
import com.taoyuan.hotel.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao dao;
    @Override
    public R save(Order order) {
        if (dao.add(order)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean all(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Order> pageInfo=new PageInfo<>(dao.all());
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(Integer id) {
        if (id > 0) {
            if (dao.del(id) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R update(Order order) {
        if (dao.update(order)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean selectAny(Integer rid, Integer roid, int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Order> pageInfo=new PageInfo<>(dao.selectWhere(rid, roid));
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        if (ids!=null && ids.length>0){
            if (dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public PageBean select(String name, int page, int limit) {
        if (!StringUtils.hasLength(name)){
            name=null;
        }else {
            name="%"+name+"%";
        }
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Order> pageInfo=new PageInfo<>(dao.select(name));
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }
}
